# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

package require tcltest 2
namespace import tcltest::*

set pwd [file dirname [file normalize $argv0]]

source ../port_test_autoconf.tcl
package require macports 1.0

array set ui_options {}
#set ui_options(ports_debug)   yes
#set ui_options(ports_verbose) yes
mportinit ui_options

package require portpatch
source ../port_autoconf.tcl
source ./library.tcl
macports_worker_init


test build_getpatchtype {
    Get patch type unit test.
} -body {
    set res [portpatch::build_getpatchtype]
    if {$res != $portutil::autoconf::patch_path} {
        return "FAIL: wrong path"
    }
    return "Get patch type successful."
} -result "Get patch type successful."


test patch_main {
    Patch main unit test.
} -constraints {
    root
} -setup {
    set macosx_version 12

    set subport gcc_select
    set filespath $pwd/files
    set distpath $pwd/dist

    set workpath $pwd/work
    set worksrcpath $workpath/src
    set compiler.cpath /usr/bin/gcc
    set compiler.library_path /usr/lib
    set macosx_deployment_target $pwd/deploy_target

    file mkdir $filespath
    set fd [open $filespath/file.diff w+]
    puts $fd "first line should get to the workpath"
    close $fd

    set mport [mportopen file://.]
    set workername [ditem_key $mport workername]
    $workername eval "set patchfiles {file.diff}"

} -body {
    if {[$workername eval eval_targets patch]} {
        return "FAIL: cannot run patch"
    }
    if {![file exists $pwd/work/select-0.1/test_patch]} {
        return "FAIL: missing patched file"
    }
    return "Patch successful."

} -cleanup {
    file delete -force $filespath
    file delete -force $pwd/work

} -result "Patch successful."


cleanupTests
